﻿using SD.Common.PoweredByLee;
using SlamDunk.CMS.DAL;
using SlamDunk.CMS.Model.DataModel;
using System;
using System.Collections.Generic;

namespace SlamDunk.CMS.BLL
{
    public class UserBll
    {
        #region 01.实例化数据层
        /// <summary>
        /// 实例化数据层
        /// </summary>
        private readonly UserDal CurrentDal = new UserDal();
        #endregion

        #region 02.添加
        /// <summary>
        /// 添加一个实体对象
        /// </summary>
        /// <param name="user">构造好要添加的实体对象</param>
        /// <returns>添加完毕后的实体对象</returns>
        public User Add(User user)
        {
            user.Password = user.Password.ToMD5();      //密码需MD5加密
            return CurrentDal.Add(user);
        }
        #endregion

        #region 03.删除（物理删除）
        /// <summary>
        /// 删除一个实体对象
        /// </summary>
        /// <param name="id">要删除的实体对象Id</param>
        /// <returns>删除结果</returns>
        public bool PhysicalDelete(int id)
        {
            return CurrentDal.PhysicalDelete(id) > 0;
        }
        #endregion

        #region 05.修改
        /// <summary>
        /// 修改一个实体对象
        /// </summary>
        /// <param name="user">构造好的要修改的实体对象</param>
        /// <returns>修改结果</returns>
        public bool Update(User user)
        {
            //判断用户是否修改了密码
            string oldPwd = CurrentDal.GetModel(user.Id).Password;
            if (user.Password == oldPwd)
            {
                return CurrentDal.Update(user) > 0;
            }
            else
            {
                user.Password = user.Password.ToMD5();
                return CurrentDal.Update(user) > 0;
            }
        }
        #endregion

        #region 06.根据主键获取单个实体对象
        /// <summary>
        /// 根据主键获取单个实体对象
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>单个实体对象</returns>
        public User GetModel(int id)
        {
            return CurrentDal.GetModel(id);
        }
        #endregion

        #region 08.获取总记录条数
        /// <summary>
        /// 返回总记录条数
        /// </summary>
        /// <returns>总记录条数</returns>
        public int GetCount()
        {
            return CurrentDal.GetCount();
        }
        #endregion

        #region 09.分页获取实体对象集合
        /// <summary>
        /// 分页获取实体对象集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="rowCount">总记录条数</param>
        /// <returns>实体对象集合</returns>
        public List<User> GetModelList(int pageIndex, int pageSize, out int pageCount, out int rowCount)
        {
            //1.计算总记录条数与总页数
            rowCount = this.GetCount();
            pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
            //2.页索引处理
            pageIndex = pageIndex < 1 ? 1 : pageIndex;
            pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
            //3.计算起始行与终止行
            int start = (pageIndex - 1) * pageSize + 1;
            int end = pageIndex * pageSize;
            //4.调用数据层返回相应实体集合
            return CurrentDal.GetModelList(start, end);
        }
        #endregion

        #region 01.根据条件获取记录条数
        /// <summary>
        /// 根据条件获取记录条数
        /// </summary>
        /// <param name="arg1">条件参数</param>
        /// <param name="arg2">条件参数</param>
        /// <param name="arg3">条件参数</param>
        /// <param name="arg4">条件参数</param>
        /// <returns>记录条数</returns>
        public int GetCount(string arg1, string arg2, string arg3, string arg4)
        {
            //1个参数
            if (!string.IsNullOrWhiteSpace(arg1) && string.IsNullOrWhiteSpace(arg2) && string.IsNullOrWhiteSpace(arg3) && string.IsNullOrWhiteSpace(arg4))
            {
                return this.CurrentDal.GetCount("Field1", arg1);
            }

            //2个参数
            if (!string.IsNullOrWhiteSpace(arg1) && !string.IsNullOrWhiteSpace(arg2) && string.IsNullOrWhiteSpace(arg3) && string.IsNullOrWhiteSpace(arg4))
            {
                return this.CurrentDal.GetCount("Field1", arg1, "Field2", arg2);
            }

            //3个参数
            if (!string.IsNullOrWhiteSpace(arg1) && !string.IsNullOrWhiteSpace(arg2) && !string.IsNullOrWhiteSpace(arg3) && string.IsNullOrWhiteSpace(arg4))
            {
                return this.CurrentDal.GetCount("Field1", arg1, "Field2", arg2, "Field3", arg3);
            }

            //4个参数
            return this.CurrentDal.GetCount("Field1", arg1, "Field2", arg2, "Field3", arg3, "Field4", arg4);
        }
        #endregion

        #region 03.校验用户登录
        /// <summary>
        /// 校验用户登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="model">用户实体对象</param>
        /// <returns>登录结果</returns>
        public bool CheckLogin(string userName, string password, out User model)
        {
            model = this.CurrentDal.GetModel(userName);
            if (model == null)
            {
                return false;
            }
            if (model.Password != password.ToMD5())
            {
                return false;
            }
            return true;
        }
        #endregion

        #region 04.校验Cookie登录
        /// <summary>
        /// 校验Cookie登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="safePwd">MD5加密后的密码</param>
        /// <param name="model">用户实体对象</param>
        /// <returns>登录结果</returns>
        public bool CheckCookieLogin(string userName, string safePwd, out User model)
        {
            model = this.CurrentDal.GetModel(userName);
            if (model == null || model.Password != safePwd)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region 05.修改密码
        /// <summary>
        /// 修改密码方法
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="oldPwd">旧密码</param>
        /// <param name="newPwd">新密码</param>
        /// <returns>修改结果</returns>
        public bool UpdatePassword(int userId, string oldPwd, string newPwd)
        {
            //1.根据给定Id查询用户实体
            User user = this.CurrentDal.GetModel(userId);
            //2.判断实体是否为空
            if (user != null)       //实体不为空
            {
                //3.判断旧密码是否正确
                if (oldPwd.ToMD5() == user.Password)        //旧密码正确
                {
                    //4.修改密码
                    user.Password = newPwd.ToMD5();     //MD5加密后重新存入数据库
                    return this.CurrentDal.Update(user) > 0;
                }
                else            //旧密码不正确
                {
                    return false;
                }
            }
            else                //实体为空
            {
                return false;
            }
        }
        #endregion
    }
}
